package com.android.fragmentdemo.fragments
import android.content.Intent
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import android.util.Log
import android.view.View
import com.android.fragmentdemo.AutoWrapViewGroupActivity
import com.android.fragmentdemo.BinderDataTransfer
import com.android.fragmentdemo.CardItemActivity
import com.android.fragmentdemo.DefineNestedScrollActivity
import com.android.fragmentdemo.DynamicAddViewActivity
import com.android.fragmentdemo.DynamicContainer2Activity
import com.android.fragmentdemo.DynamicContainerActivity
import com.android.fragmentdemo.FiledConstant
import com.android.fragmentdemo.HorizontalCustomLayoutManagerActivity
import com.android.fragmentdemo.LooperImgActivity
import com.android.fragmentdemo.LooperTipActivity
import com.android.fragmentdemo.NestedScrollViewActivity
import com.android.fragmentdemo.PullToRefreshActivity
import com.android.fragmentdemo.PullUpToLoadActivity
import com.android.fragmentdemo.R
import com.android.fragmentdemo.RVWithHeaderActivity
import com.android.fragmentdemo.ScaleOverloadingActivity
import com.android.fragmentdemo.ScreenPixelActivity
import com.android.fragmentdemo.Scrolling2Activity
import com.android.fragmentdemo.ScrollingActivity
import com.android.fragmentdemo.ShadowFloatActivity
import com.android.fragmentdemo.SnapHelperActivity
import com.android.fragmentdemo.StaggeredActivity
import com.android.fragmentdemo.StereoActivity
import com.android.fragmentdemo.StudyLayoutManagerActivity
import com.android.fragmentdemo.TurntableActivity
import com.android.fragmentdemo.VerticalCustomLayoutManagerActivity
import com.android.fragmentdemo.ViewInCenterActivity
import com.android.fragmentdemo.ViewPager2UseActivity
import com.android.fragmentdemo.adapters.CustomLayoutManagerAdapter
import com.hengfoxwolf.commonbase.base.BaseFragment
import com.hengfoxwolf.commonbase.base.databeans.BinderData
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.FragmentMessageBinding
import com.android.fragmentdemo.definervlayoutmanager.CustomLayoutManager4
import com.android.fragmentdemo.startActivity
import com.hengfoxwolf.commonbase.base.adapter.BaseRvAdapter

class MessageFragment : BaseFragment<FragmentMessageBinding>() {

    private lateinit var viewModel: MessageViewModel

    private lateinit var mInteractionModel: FragmentsInteractionModel

    override fun getLayoutId() = R.layout.fragment_message

    override fun init(savedInstanceState: Bundle?) {
        arguments.let {
            //val title = it?.getString(KEY_PARAM) ?: ""
            //mBinding.tvMessage.text = title
        }

//        val subView =
//            LayoutInflater.from(context)
//                .inflate(R.layout.list_data_rv_sub_item, mBinding.rootMessage, false)
//        mBinding.rootMessage.addView(
//            subView,
//            FrameLayout.LayoutParams(
//                FrameLayout.LayoutParams.MATCH_PARENT,
//                FrameLayout.LayoutParams.WRAP_CONTENT
//            )
//        )
//
//        subView.findViewById<FrameLayout>(R.id.sub_item_root).setOnClickListener {
//            Log.d(TAG,"$TAG---FrameLayout")
//        }

        val mAdapter = CustomLayoutManagerAdapter()
        mAdapter.setItemDataSet(DataFactoryIp.createSimpleItemData(26))
        mAdapter.setOnItemClickListener(object : BaseRvAdapter.IOnItemClickListener<String> {
            override fun onItemClick(dataItem: String, position: Int, viewId: Int) {
                Log.d(TAG, "position:${position}")
                when (position) {
                    0 -> {
                        mContext.startActivity<CardItemActivity>()
                    }
                    1 -> {
                        mContext.startActivity<ViewPager2UseActivity>()
                    }
                    2 -> {
                        mContext.startActivity<HorizontalCustomLayoutManagerActivity>()
                    }
                    3 -> {
                        mContext.startActivity<StaggeredActivity>()
                    }
                    4 -> {
                        mContext.startActivity<StudyLayoutManagerActivity>()
                    }
                    5 -> {
                        mContext.startActivity<TurntableActivity>()
                    }
                    6 -> {
                        mContext.startActivity<SnapHelperActivity>()
                    }
                    7 -> {
                        mContext.startActivity<ShadowFloatActivity>()
                    }
                    8 -> {
                        mContext.startActivity<StereoActivity>()
                    }
                    9 -> {
                        mContext.startActivity<ViewInCenterActivity>()
                    }
                    10 -> {
                        mContext.startActivity<DynamicAddViewActivity>()
                    }
                    11 -> {
                        mContext.startActivity<ScreenPixelActivity>()
                    }
                    12 -> {
                        mContext.startActivity<DynamicContainerActivity>()
                    }
                    13 -> {
                        mContext.startActivity<DynamicContainer2Activity>()
                    }
                    14 -> {
                        mContext.startActivity<RVWithHeaderActivity>()
                    }
                    15 -> {
                        mContext.startActivity<ScrollingActivity>()
                    }
                    16 -> {
                        mContext.startActivity<PullUpToLoadActivity>()
                    }
                    17 -> {
                        mContext.startActivity<PullToRefreshActivity>()
                    }
                    18 -> {
                        mContext.startActivity<NestedScrollViewActivity>()
                    }
                    19 -> {
                        val binderDataTf = BinderDataTransfer()
                        binderDataTf.mDataSet = createData()
                        val binderBundle = Bundle()
                        binderBundle.putBinder(FiledConstant.BINDER_DATA_LIST, binderDataTf)
                        val binderIntent = Intent(requireContext(), DefineNestedScrollActivity::class.java)
                        binderIntent.putExtras(binderBundle)
                        requireContext().startActivity(binderIntent)
                        //mContext.startActivity<DefineNestedScrollActivity>()
                    }
                    20 -> {
                        mContext.startActivity<AutoWrapViewGroupActivity>()
                    }
                    21 -> {
                        mContext.startActivity<VerticalCustomLayoutManagerActivity>()
                    }
                    22 -> {
                        mContext.startActivity<LooperTipActivity>()
                    }
                    23 -> {
                        mContext.startActivity<Scrolling2Activity>()
                    }
                    24 -> {
                        mContext.startActivity<ScaleOverloadingActivity>()
                    }
                    25 -> {
                        mContext.startActivity<LooperImgActivity>()
                    }
                    else -> {}
                }
            }
        })
        val mLayoutManager = CustomLayoutManager4(5)
        mBinding.rvMessage.apply {
            layoutManager = mLayoutManager
            //layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
            //addItemDecoration(ListDataItemDecoration(context))
        }
        //mLayoutManager.scrollToPosition(2, true)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProvider(this)[MessageViewModel::class.java]

        //ViewModel共用一个ViewModelStoreOwner实例实现共享数据
        mInteractionModel =
            ViewModelProvider(requireActivity())[FragmentsInteractionModel::class.java]
        Log.d(TAG,"${TAG}---${mInteractionModel.number.value}")
    }

    private fun createData() : MutableList<BinderData> {
        val dataSet = mutableListOf<BinderData>()
        for (i in 0.until(1000)) {
            dataSet.add(BinderData(i + 1))
        }
        return dataSet
    }

    companion object {
        private const val TAG = "MessageFragment"
        private const val KEY_PARAM = "message"

        fun newInstance(param: String): MessageFragment {
            val fragment = MessageFragment()
            val bundle = Bundle()
            bundle.putString(KEY_PARAM, param)
            fragment.arguments = bundle
            return fragment
        }
    }
}